/**
 * 
 */
package com.game.entity;

import java.awt.Polygon;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import com.game.entity.generic.BaseVectorShape;
import com.game.util.Util;

/**
 * @author gustavo
 * 
 */
public class Ship extends BaseVectorShape {

	// Definindo os poligonos da nave
	private int[] shipx = { -6, -3, 0, 3, 6, 0 };
	private int[] shipy = { 6, 7, 7, 7, 6, -7 };
	private List<String> message;
	private List<Bullet> bullets;
	private int totalBullets;
	private int currentBullet;
	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Override
	public Rectangle getBounds() {
		return new Rectangle((int) getX() - 6, (int) getY() - 6, 12, 12);
	}

	public Ship() {
		setShape(new Polygon(shipx, shipy, shipx.length));
		setAlive(true);
		message = new ArrayList<String>();
		bullets = new ArrayList<Bullet>();
		currentBullet = -1;
		totalBullets = 10;
		init();
	}

	public void fireEvent() {
		currentBullet++;
		if (currentBullet > totalBullets - 1)
			currentBullet = 0;
		bullets.get(currentBullet).setAlive(true);
		// point bullet in same direction ship is facing
		bullets.get(currentBullet).setX(this.getX());
		bullets.get(currentBullet).setY(this.getY());
		bullets.get(currentBullet).setMoveAngle(this.getFaceAngel() - 90);

		// fire bullet at angle of the ship
		double angle = bullets.get(currentBullet).getMoveAngle();
		double svx = this.getVelX();
		double svy = this.getVelY();
		bullets.get(currentBullet)
				.setVelX(svx + Util.calcAngleMoveX(angle) * 2);
		bullets.get(currentBullet)
				.setVelY(svy + Util.calcAngleMoveY(angle) * 2);
	}

	public void updateMoveBulletsEvent(double width, double height) {
		// move each of the bullets
		for (Bullet bullet : this.getBullets()) {
			// is this bullet begins used?
			if (bullet.isAlive()) {
				// update bullet`s X positions
				bullet.incX(bullet.getVelX());

				// bullet disappears at left/right edge
				if (bullet.getX() < 0 || bullet.getX() > width) {
					bullet.setAlive(false);
				}

				// update bullets Y position
				bullet.incY(bullet.getVelY());

				// bullet disappears at top/buttum edge
				if (bullet.getY() < 0 || bullet.getY() > height) {
					bullet.setAlive(false);
				}
			}
		}
	}

	public void moveShipToLeft() {
		this.incFaceAngel(-5);
	}

	public void moveShipToRight() {
		this.incFaceAngel(5);
	}

	public void moveShipToUp() {
		this.setMoveAngle(this.getFaceAngel() - 90);
		this.incVelX(Util.calcAngleMoveX(this.getMoveAngle()) * 0.1);
		this.incVelY(Util.calcAngleMoveY(this.getMoveAngle()) * 0.1);
	}

	public void moveShipToDown() {
		this.setMoveAngle(this.getFaceAngel() - 90);
		this.dimVelX(Util.calcAngleMoveX(this.getMoveAngle()) * 0.1);
		this.dimVelY(Util.calcAngleMoveY(this.getMoveAngle()) * 0.1);
	}

	public void updateMoveShipEvent(double width, double height) {
		// update ships X position
		this.incX(this.getVelX());
		// Wrap around leff/right
		if (this.getX() < -10) {
			this.setX(width + 10);
		} else if (this.getX() > width + 10) {
			this.setX(-10);

		}

		// update ship Y position
		this.incY(this.getVelY());
		// Wrap around top/button
		if (this.getY() < -10) {
			this.setY(height + 10);
		} else if (this.getY() > height + 10) {
			this.setY(-10);

		}

	}

	public List<Bullet> getBullets() {
		return bullets;
	}

	public void setBullets(List<Bullet> bullets) {
		this.bullets = bullets;
	}

	public int getTotalBullets() {
		return totalBullets;
	}

	public void setTotalBullets(int totalBullets) {
		this.totalBullets = totalBullets;
	}

	public int getCurrentBullet() {
		return currentBullet;
	}

	public void setCurrentBullet(int currentBullet) {
		this.currentBullet = currentBullet;
	}

	public void init() {
		for (int i = 0; i < totalBullets; i++) {
			bullets.add(new Bullet());
		}
	}

	public List<String> getMessage() {
		return message;
	}

	public void setMessage(List<String> message) {
		this.message = message;
	}

}
